#ifndef __ROM_DEF_H__
#define __ROM_DEF_H__

#include <stdint.h>

#include "sdk_core/rc24xx.h"

#include "sdk_core/udk_support_def/rt_def.h"
#include "sdk_core/udk_support_def/alg_def.h"
#include "sdk_core/udk_support_def/dp_def.h"
#include "sdk_core/udk_support_def/rf_def.h"
#include "sdk_core/udk_support_def/func_def.h"
#include "sdk_core/udk_support_def/cfg_def.h"
#include "sdk_core/udk_support_def/udk_def.h"

#define TRUE  (1)
#define FALSE (0)

#define SYS_ALIGN(x, n) ((x + (n-1)) & ~(n-1))

/* SDK VERSION */
#define SDK_VERSION_GENE(x,y) (x*64+y)

#define SDK_VERSION (SDK_VERSION_GENE(3,0))
#define SDK_VERSION_APPEND    "24052411" // 不使用编译时间，sdk内手动填写，确保重复编译生成bin文件一致

/*CPU SRAM*/
#define UDP_ADDR                      (0x20002000)
#define UDP_SIZE                      (0x0800)
#define UDP_RSV_SIZE                  (sizeof(PARAM_USR_RSV))

#define UDF_ADDR                      (0x20000800)
#define UDF_SIZE_MAX                  (0x1800)

#define IRQ_HANDLE_SRAM_START         (0x20000680)
#define IRQ_HANDLE_SRAM_END           (0x20000800)

#define RAM_USER_START			          (0x20000680)
#define RAM_USER_END			            (0x20002800)

#define CPU_SRAM_START			          (0x20000000)
#define CPU_SRAM_END			            (0x20002800)
#define CPU_SRAM_SIZE                 (CPU_SRAM_END-CPU_SRAM_START)

/* BBE SRAM*/
#define BBE_SRAM_START                (0x30000000)
#define BBE_SRAM_END                  (0x3000A000)

#define BBE_SRAM_SYSBOOT_START        (0x30000000)
#define BBE_SRAM_SYSBOOT_SIZE         (6*1024)
#define BBE_SRAM_SYSBOOT_END          (BBE_SRAM_SYSBOOT_START+BBE_SRAM_SYSBOOT_SIZE)

#define BBE_SRAM_UDK_SDL_SIZE         (10*1024)
#define BBE_SRAM_UDK_FREE_SIZE        (16*1024)

#define BBE_SRAM_UDK_START            (BBE_SRAM_SYSBOOT_END)
#define BBE_SRAM_UDK_SDL_START        (BBE_SRAM_UDK_START)
#define BBE_SRAM_UDK_SDL_END          (BBE_SRAM_UDK_SDL_START+BBE_SRAM_UDK_SDL_SIZE)
#define BBE_SRAM_UDK_FREE_START       (BBE_SRAM_UDK_SDL_END)
#define BBE_SRAM_UDK_FREE_END         (0x30008000)
#define BBE_SRAM_UDK_END              (BBE_SRAM_UDK_FREE_END)


/*NVM*/
#define UDT_OFFSET_UDP                (UDP_SIZE+512)
#define UDT_SIZE_MAX                  (512)
#define USRRSV_SIZE_MAX               (1024)
#define APP_HEAD_SIZE                 (4096) // UDP+SIGN+UDT+RSV 共4K

#define FLASH_SECTOR_SIZE             (0x1000)
#define FLASH_PAGE_SIZE               (256)
#define FLASH_PAGE_PER_SECTOR         (16)

#define SYSBIN_SIZE_MAX               (0xC000) // 48KB

#define UDT_MAX_NUM                   (17)

/*IRQ*/
#define JUMP_IRQ_HANDLE(x)      ((void (*)(int))(IRQ_HANDLE_SRAM_START+1))(x)

/*SYSBOOT 内定义*/
/*APP源(内部/外部/SRAM)*/
#define APP_LOCATION_INNER  (0)
#define APP_LOCATION_OUTER  (1)
#define APP_LOCATION_SRAM   (2)

/*CFG位置*/
#define CFG_PLACE_SYS       (0)
#define CFG_PLACE_USR_NONE  (CFG_PLACE_SYS)
#define CFG_PLACE_USR_A     (1)
#define CFG_PLACE_USR_B     (2)

/*APP位置(默认/A/B)*/
#define APP_PLACE_DEFAULT   (0)
#define APP_PLACE_A_AREA    (1)
#define APP_PLACE_B_AREA    (2)

/*启动标识*/
#define ROM_STARTUP         (0)
#define XIP_STARTUP         (1)

/*SYSBOOT UDK 约定*/
// XIP 0~48K 定义为内部 48K以外定义为外部
// ROM 0~48K 定义为内部 FLASH定义为外部
#define SYSBOOT_APP_BASE_INNER          (40*1024-256)  // 32KB @ sys-bus
#define SYSBOOT_APP_BASE_OUTER          (24*1024)      // rom: 24KB  periph-bus ; xip 24KB+SYSBIN_SIZE_MAX  sys-bus
#define SYSBOOT_ACTIVATE_OUTER          (0*1024)       // @0KB
#define SYSBOOT_ACTIVATE_OUTER_SIZE     (4*1024)       // 4KB
#define SYSBOOT_KEY_OUTER               (0*1024)       // @4KB
#define SYSBOOT_KEY_OUTER_SIZE          (4*1024)       // 4KB
#define SYSBOOT_AB_CFG_OUTER            (8*1024)       // @8KB
#define SYSBOOT_AB_CFG_PER_SIZE         (4*1024)       // 4KB
#define SYSBOOT_AB_INFO_OUTER           (16*1024)      // @16KB
#define SYSBOOT_AB_INFO_PER_SIZE        (4*1024)       // 4KB

#define ADDR_SYS_CFG                    (0xBF00)
#define SIZE_SYS_CFG                    (0x100)

#define SIGNATURE_RSV_OFFSET            (0x800)     // 偏移2KB
#define APP_RSV_OFFSET                  (0xC00)     // 偏移3KB



/*位于ABUF0中临时区域，用户可通过gPara.paramStart获取指针，
可用于SYSBOOT、UDF01、UDF02、UDF15、通用UDF之间数据共享/传递等。
指向内容结构rsv用户可重新定义部分*/
typedef struct{
	uint32_t addr;         // 地址
	uint32_t size;         // 大小
	uint32_t app_location; // 0-INNER 1-rom/xip(OUTER) 2-bbsram
	uint16_t crc_num;      // MAX:25
	uint16_t crc[25];      // UDP 6个crc UDT+RSV 1个 OPT_DATA 1个 udf最多16个 预留1个
}app_digest_t;

typedef struct{
	app_digest_t app_digest;

	uint32_t rsv[(2048-sizeof(app_digest_t))/4];
}STARTUP_PARAM;       // 注：UDK内参照此处重新定义为start_2k_t

#define STARTUP_PARAM_SIZE (2048)

#define UNIVERSAL_UDF_CALL(x) (*((int(*)())(x)))()
typedef int (*UDF01_02_FUNCTION)(uint8_t id,app_digest_t *p_app_digest , uint8_t *data,int* status);

#define UNIVERSAL_UDF_START                     ( 5-1) // udf05~udf14 通用udf
#define UNIVERSAL_UDF_END                       (14-1)
#define UNIVERSAL_UDF_RES_UDFMIN                (UNIVERSAL_UDF_RES_GO_UDF05)
#define UNIVERSAL_UDF_RES_UDFMAX                (UNIVERSAL_UDF_RES_GO_UDF14)

// 注未知返回值按UNIVERSAL_UDF_RES_NORMAL处理
#define UNIVERSAL_UDF_RES_RESET                 (-1)// 重置调度状态，按序调度
#define UNIVERSAL_UDF_RES_NORMAL                (0) // 按序调度
#define UNIVERSAL_UDF_RES_GO_UDF05              (1) // 调度跳转至UDF05
#define UNIVERSAL_UDF_RES_GO_UDF06              (2) // 调度跳转至UDF06
#define UNIVERSAL_UDF_RES_GO_UDF07              (3) // 调度跳转至UDF07
#define UNIVERSAL_UDF_RES_GO_UDF08              (4) // 调度跳转至UDF08
#define UNIVERSAL_UDF_RES_GO_UDF09              (5) // 调度跳转至UDF09
#define UNIVERSAL_UDF_RES_GO_UDF10              (6) // 调度跳转至UDF10
#define UNIVERSAL_UDF_RES_GO_UDF11              (7) // 调度跳转至UDF11
#define UNIVERSAL_UDF_RES_GO_UDF12              (8) // 调度跳转至UDF12
#define UNIVERSAL_UDF_RES_GO_UDF13              (9) // 调度跳转至UDF13
#define UNIVERSAL_UDF_RES_GO_UDF14              (10)// 调度跳转至UDF14
#define UNIVERSAL_UDF_RES_CNT                   (11)
#define UNIVERSAL_UDF_RES_NORMAL_NO_NEED_LOAD   (UNIVERSAL_UDF_RES_NORMAL   + UNIVERSAL_UDF_RES_CNT ) // 按序调度 , 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF05_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF05 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF05, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF06_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF06 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF06, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF07_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF07 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF07, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF08_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF08 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF08, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF09_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF09 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF09, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF10_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF10 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF10, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF11_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF11 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF11, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF12_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF12 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF12, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF13_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF13 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF13, 下次无需加载
#define UNIVERSAL_UDF_RES_GO_UDF14_NO_NEED_LOAD (UNIVERSAL_UDF_RES_GO_UDF14 + UNIVERSAL_UDF_RES_CNT ) // 调度跳转至UDF14, 下次无需加载
#define UNIVERSAL_UDF_RES_MAX                   (UNIVERSAL_UDF_RES_CNT*2-1)


#define UDF01_UDF02_RES_COMPLETE                (0)
#define UDF01_UDF02_RES_INCOMPLETE              (-1)



typedef struct
{
	uint32_t RSV[30];
	uint16_t crcLen;
	uint16_t crcVal;
}PARAM_USR_RSV;

typedef struct{
	PARAM_ANA_VENUS paramANA;
	PARAM_DATAPREP  paramDataPrep;
	PARAM_ALGORITHM paramALG;
	PARAM_FUNC      paramFunc;
	PARAM_RUNTIME   paramRT;
	PARAM_USR_RSV   param_USR_RSV;
}param_2k_t;      // 注: UDK内重新参照此处重新定义为UDP_2K_t

typedef struct{  // 4K大小
	sys_cfg_t     sys_cfg;  /* 必须保留，SYSBOOT依赖 */
	
	/*后续定义仅示意,SYSBOOT未使用*/	
	uint32_t      cfg_data[4096/4-20-1]; // cfg_data

	/* 1 DWORD */
	uint16_t      crcLen;           
	uint16_t      crcVal;
}cfg_opt_def_t;   // 注：UDK内参照此处重新定义为UDC_def_t


#define UART_REC_MAX_LEN    (50)

typedef struct{
	sys_cfg_t        sys_cfg;
	uint32_t         rsv[64-20-1];
	uint32_t         image_crc;
}SYSCFG_PARAM;

struct uart_recv
{
uint8_t RecCount; // 4
uint32_t RecLen;  // 4
uint8_t RecFinish;
uint8_t RecData;
uint8_t RecBuff[UART_REC_MAX_LEN];
uint8_t timeoutStart; // 56
uint32_t timeoutCnt;  // 4
uint32_t writeCnt;    // 4
};
	
typedef struct
{
	struct uart_recv strUart;

	uint8_t version;        // ROM版本 // 和老rom兼容，老rom此处根据gpio0.0可取0/1/2
	uint8_t boot_flag;      // 启动标识
	uint8_t debug_en;       // 调试开关
	uint8_t use_inner_app;  // 使用内置app
	
	uint8_t printen;        
	uint8_t app_location;   // app位置
	uint8_t flash_mid;      // flash: manufacturer
	uint8_t flash_did;      // flash: device id
	
	uint8_t flash_uid[16];  // flash uid

	const SYSCFG_PARAM * p_sys_rsv; // 系统配置区
	STARTUP_PARAM* paramStart;      // 交换共享区域指针
	param_2k_t * p_2k;              // udp参数指针

	uint32_t SysTick_cnt;
	float rtcCalibRtnClk;	
	
	uint32_t cfg_ab_sn;      // cfg a/b sn

	uint8_t flash_init_flag; // flash 初始化 flag
	uint8_t cfg_place;       //  a/b区/none
	uint8_t app_id;          // 应用选择id
	uint8_t app_place;       // a/b区/default
	
	uint32_t app_ab_sn;      // app a/b sn
	uint32_t app_offset;     // app_offset: UDK内编译存储地址为SYSBOOT_APP_BASE_INNER/OUTER。A/B区 或 多APP,存储位置偏移量。
	
	uint32_t startup;
	uint8_t  has_app;
	uint8_t  normal;
	uint8_t  user_para0;     // 预留变量0 for UDF用户，独立应用切换时传递状态等
	uint8_t  user_para1;     // 预留变量1 for UDF用户，独立应用切换时传递状态等
	int (*user_function)(int *);
	char *   version_append;
}GLOBAL_PARAM;

#endif
